﻿namespace USElections.Commands
{
    using System;
    using System.Windows.Input;

    /// <summary>
    /// OpenWebBrowserCommand class.
    /// </summary>
    public class OpenWebBrowserCommand : ICommand
    {
        #region Events
        /// <summary>
        /// CanExecuteChanged event.
        /// </summary>
        public event EventHandler CanExecuteChanged;
        #endregion

        #region Public methods
        /// <summary>
        /// Can execute command.
        /// </summary>
        /// <param name="parameter">Parameter not used.</param>
        /// <returns>True (always).</returns>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Execute command.
        /// </summary>
        /// <param name="parameter">The url to open.</param>
        public void Execute(object parameter)
        {
            if (!string.IsNullOrEmpty(parameter as string))
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri(parameter as string, UriKind.Absolute)
                };

                try
                {
                    task.Show();
                }
                catch
                {
                }
            }
        }
        #endregion
    }
}
